//
//  KY_VideoDetailViewController.m
//  ShaQueVideo
//
//  Created by huliang on 13-4-26.
//  Copyright (c) 2013年 科悦信达. All rights reserved.
//

#import "KY_VideoDetailViewController.h"



@interface KY_VideoDetailViewController ()
@end

@implementation KY_VideoDetailViewController
@synthesize adView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id)initWithVideoURL:(NSString *)videoURL{
    self = [super initWithNibName:@"KY_VideoDetailViewController" bundle:nil];
    if (self) {
        self.videoURL = videoURL;
    }
    return self;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.player];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.player];

    if (self.adView.superview) {

        [self.adView removeFromSuperview];
    }
    
    [self.adView setDelegate:nil];
    self.adView = nil;
    self.player = nil;
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(myMovieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:self.player];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.player];
    self.adView = [AdViewView requestAdViewViewWithDelegate:self];
    [self.adView setFrame:CGRectMake(0, 70, self.adView.frame.size.width, self.adView.frame.size.height)];
    [self.view addSubview:self.adView];
    [self.view bringSubviewToFront:self.adView];


}
-(void)playStateChange:(NSNotification *)notification
{
    if (self.player.playbackState == MPMoviePlaybackStatePaused)
    {
        [self showGuanggao];
    }
    else if (self.player.playbackState == MPMoviePlaybackStatePlaying)
    {
        if (self.playAdRemove) {
            
                [self removeGuanggao];
    
        }
        
    }
}
-(void)removeGuanggao
{
    if (self.adView.superview) {
        
        [self.adView removeFromSuperview];
        self.playAdRemove = YES;
        
    } 
}
-(void)showGuanggao
{
    if (!self.adView.superview) {

         [self.view addSubview:self.adView];
    }
   
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //id, title, icon, playurl,  sourceurl, cid
    self.player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:self.videoURL]];
    
    self.player.movieSourceType = MPMovieSourceTypeStreaming;
    self.player.scalingMode = MPMovieScalingModeAspectFit;
    self.player.controlStyle = MPMovieControlStyleFullscreen;
    self.player.shouldAutoplay =  YES;
    self.player.fullscreen = YES;
    [self.player.view setFrame:self.view.frame];
    [self.view addSubview:self.player.view];
 
     self.player.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    
       
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(removeGuanggao) userInfo:nil repeats:NO];

        
}

- (NSString*) adViewApplicationKey {

    return @"SDK201312081206290g605c5qaeugil1";

    // 在 AdView ⺴⽹网站申请的 key.
}

- (UIViewController*) viewControllerForPresentingModalView {

    KY_AppDelegate *delegate =  [[UIApplication sharedApplication] delegate];

    return  delegate.tabBarController;

    // 全屏⼲⼴广告附着的控制器.
}
- (BOOL) adViewTestMode {

    return NO;

    //在测试时返回 YES;
    
}
-(void)myMovieFinishedCallback:(NSNotification*)aNotification{
    
    MPMoviePlayerController* theMovie=[aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:theMovie];
    
    [self dismissModalViewControllerAnimated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return  NO;
}
- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
